package com.htj.kf2b.construct_mobile.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.text.StrSplitter;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.htj.cross.util.Des;
import com.htj.cross.webservice.CrossService;
import com.htj.cross.webservice.CrossService_Service;
import org.apache.commons.collections.map.LinkedMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.tempuri.ArrayOfString;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * @param phone
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public JSONObject getUser(String phone) throws UnsupportedEncodingException {


        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getUserByPhone","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(phone);/*内外方法的参数*/
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String s = service.jGetJson(str, "construct", data);
        String ss = Base64.decodeStr(s);

        JSONObject jsonObject1 = JSONUtil.parseObj(ss);


        //模拟返回值
        LinkedMap map = new LinkedMap();
        map.put("name",jsonObject1.get("name"));
        map.put("phone",phone);
        map.put("post",jsonObject1.get("post"));
        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putAll(map);
        return jsonObject;
    }


    /**
     * 获取委托人列表
     * @param phone
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/trustee", method = RequestMethod.GET)
    public JSONObject getTrustee(String phone) {


        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getEntrustedListByPhone","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(phone);/*内外方法的参数*/
        data.getString ().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);

        String resultBase64 = Base64.decodeStr(r);


        String resultSting = resultBase64.substring(0, resultBase64.length() - 1);

        System.out.println(resultSting);

        ArrayList arrayList = new ArrayList();


        List<String> split = StrSplitter.split(resultSting, '}', 0, true, true);
        System.out.println(split);


        for (String s : split) {
            System.out.println(s.substring(1)+"}");
            arrayList.add(JSONUtil.parseObj(s.substring(1)+"}"));
        }



        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putOnce("trustee", arrayList);
        return jsonObject;
    }


//    @CrossOrigin
//    @RequestMapping(value = "/trusteefuzzy", method = RequestMethod.GET)
//    public JSONObject getTrusteeFuzzySearch(String fuzzyName) {
//
//
//        //TODO  根据模糊搜索条件获取
//
//        //模拟返回值
//        ArrayList arrayList = new ArrayList();
//        arrayList.add(fuzzyName);
//        arrayList.add("巴飞羽");
//        arrayList.add("茅语丝");
//        arrayList.add("将鹏举");
//        arrayList.add("仝全喜");
//
//
//        JSONObject jsonObject = JSONUtil.createObj();
//        jsonObject.putOnce("code", "200");
//        jsonObject.putOnce("msg", "成功");
//        jsonObject.putOnce("trustee", arrayList);
//        jsonObject.putOnce("fuzzyName", fuzzyName);
//        return jsonObject;
//    }
}